package com.aba.bms.book;

import java.util.ArrayList;
import java.util.List;

public class Book {
    private static List<Book> bookList = new ArrayList<>();
    private String isbn;
    private String title;
    private String author;
    private String category; 

    public Book(String isbn, String title, String author, String category) {
        this.isbn = isbn;
        this.title = title;
        this.author = author;
        this.category = category;
    }

    public String getIsbn() {
        return isbn;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public String getCategory() {
        return category;
    }

    @Override
    public String toString() {
        return "ISBN: " + isbn + ", 书名: " + title + ", 作者: " + author + ", 类别: " + category;
    }

    /** 添加图书 */
    public static boolean addBook(Book book) {
        // 检查ISBN是否已存在
        for (Book b : bookList) {
            if (b.getIsbn().equals(book.getIsbn())) {
                return false; // ISBN重复，添加失败
            }
        }
        return bookList.add(book);
    }

    /** 按ISBN查找图书 */
    public static Book findBookByIsbn(String isbn) {
        for (Book book : bookList) {
            if (book.getIsbn().equals(isbn)) {
                return book;
            }
        }
        return null;
    }

    /** 按书名搜索图书 */
    public static List<Book> searchByTitle(String keyword) {
        List<Book> result = new ArrayList<>();
        for (Book book : bookList) {
            if (book.getTitle().contains(keyword)) {
                result.add(book);
            }
        }
        return result;
    }

    /** 更新图书信息 */
    public static boolean updateBook(String isbn, Book newBook) {
        for (int i = 0; i < bookList.size(); i++) {
            if (bookList.get(i).getIsbn().equals(isbn)) {
                bookList.set(i, newBook);
                return true;
            }
        }
        return false;
    }

    /** 删除图书 */
    public static boolean deleteBook(String isbn) {
        for (Book book : bookList) {
            if (book.getIsbn().equals(isbn)) {
                return bookList.remove(book);
            }
        }
        return false;
    }

    /** 获取所有图书 */
    public static List<Book> getAllBooks() {
        return new ArrayList<>(bookList);
    }

    /** 给图书分类（修改图书类别） */
    public static boolean classifyBook(String isbn, String newCategory) {
        for (Book book : bookList) {
            if (book.getIsbn().equals(isbn)) {
                book.category = newCategory;
                return true;
            }
        }
        return false;
    }

    // ====================== 测试方法 ======================
    public static void main(String[] args) {
        // 1. 添加图书
        Book book1 = new Book("1001", "Java", "张三", "计算机");
        Book book2 = new Book("1002", "数据库原理", "李四", "计算机");
        System.out.println("添加图书1: " + Book.addBook(book1));
        System.out.println("添加图书2: " + Book.addBook(book2));

        // 2. 查找图书
        Book found = Book.findBookByIsbn("1001");
        System.out.println("查找图书: " + (found != null ? found : "未找到"));

        // 3. 搜索图书
        List<Book> searchResult = Book.searchByTitle("入门");
        System.out.println("搜索结果数量: " + searchResult.size());

        // 4. 更新图书
        Book updateBook = new Book("1001", "Java2", "张三", "计算机");
        System.out.println("更新图书: " + Book.updateBook("1001", updateBook));

        // 5. 删除图书
        System.out.println("删除图书: " + Book.deleteBook("1002"));

        // 6. 给图书分类
        System.out.println("给图书分类: " + Book.classifyBook("1001", "编程语言"));

        // 7. 查看所有图书
        System.out.println("所有图书:");
        for (Book book : Book.getAllBooks()) {
            System.out.println(" " + book);
        }
    }
}
